package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.Family;
import com.zero.entity.FamilyMember;
import com.zero.mapper.FamilyMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FamilyMemberService {

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WebService webService;

    /**
     * 查询所有用户信息
     * @param param
     * @return
     */
    public Result getAllFamilyMembers(FamilyMember param){

        //设置分页信息
        if (param != null && param.getPage() != null ){
            if (param.getLimit() != null)
                PageHelper.startPage(param.getPage(), param.getLimit());
            else
                PageHelper.startPage(param.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<FamilyMember> result = familyMemberMapper.select(param);

        for (FamilyMember member : result) {
            Family family = familyMapper.selectById(member.getFamilyId());
            member.setFamilyName(family.getName());
        }

        PageInfo pageInfo = new PageInfo(result);
        return Result.ok(result, pageInfo.getTotal());
    }

    /**
     * 获得一个家庭的所有成员信息
     * @param request web请求
     * @param packet 请求的分页信息
     * @return
     */
    public Result getFamilyMembers(HttpServletRequest request, FamilyMember packet){
        log.info("packet=" + packet);
        //验证是否能拿到用户个人信息
        FamilyMember info = (FamilyMember) request.getAttribute("familyMember");
        if (info == null || info.getId() == null)
            return Result.fail("获取不到个人信息");

        //查询用户的家庭id
        FamilyMember familyMember = familyMemberMapper.selectById(info.getId());
        FamilyMember param = new FamilyMember();
        param.setFamilyId(familyMember.getFamilyId());
        if (packet != null && packet.getName() != null)
            param.setName(packet.getName());

        //设置分页信息
        if (packet.getPage() != null ){
            if (packet.getLimit() != null)
                PageHelper.startPage(packet.getPage(), packet.getLimit());
            else
                PageHelper.startPage(packet.getPage(), 10);
        }

        log.info("param=" + param.toString());
        //查询家庭成员的信息并返回
        List<FamilyMember> familyMembers = familyMemberMapper.select(param);
        PageInfo pageInfo = new PageInfo(familyMembers);
        return Result.ok( familyMembers, pageInfo.getTotal());
    }

    /**
     * 添加一个家庭成员
     * @param familyMember
     * @param request
     * @return
     */
    @Transactional
    public Result addMember(FamilyMember familyMember, HttpServletRequest request) {
        //验证是否能拿到用户个人信息
        FamilyMember info = (FamilyMember) request.getAttribute("familyMember");
        if (info == null || info.getId() == null)
            return Result.fail("获取不到个人信息");

        //查询账号是否已被注册
        FamilyMember param = new FamilyMember();
        param.setAccount(familyMember.getAccount());
        log.debug(param.toString());
        List<FamilyMember> rs = familyMemberMapper.select(param);
        log.debug(rs.size()+"");
        if (rs.size() >= 1)
            return Result.fail("该帐号已被注册，请注册其他帐号");

        //获取创建者的个人信息
        FamilyMember familyMember1 = familyMemberMapper.selectById(info.getId());

        //设置新成员的家庭id，并注册新的成员
        familyMember.setFamilyId(familyMember1.getFamilyId());
        familyMember.setCreateDate(new Date());
        familyMember.setType(1);
        familyMember.setDebt(new BigDecimal(0));
        familyMember.setAssets(new BigDecimal(0));

        int row = familyMemberMapper.add(familyMember);
        if (row == 1){
            webService.updateWebUserCount(0);
            return Result.ok("添加成功");
        }

        return Result.fail("添加失败");
    }

    /**
     * 修改家庭成员信息
     * @param familyMember
     * @param request
     * @return
     */
    public Result updateMember(FamilyMember familyMember, HttpServletRequest request){
        if (familyMember == null)
            return Result.fail("接收修改数据失败");
        log.info(familyMember.toString());

        //当没传帐号id时表明要修改自己的信息
        if (familyMember.getId() == null){
            FamilyMember info = (FamilyMember) request.getAttribute("familyMember");
            if (info == null || info.getId()==null)
                return Result.fail("获取不到用户id");
            familyMember.setId(info.getId());
        }

        familyMember.setUpdateDate(new Date());
        int row = familyMemberMapper.update(familyMember);
        if (row == 1)
            return Result.ok("修改成功");
        return Result.fail("修改失败");
    }

    /**
     * 删除ids中用,分隔的id家庭成员
     * @param ids
     * @return
     */
    @Transactional
    public Result deleteMember(String ids){
        if (ids == null)
            return Result.fail("没有接收到要删除的项");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            row += familyMemberMapper.delete(Integer.parseInt(id));
            webService.updateWebUserCount(1);
        }
        return Result.ok("成功删除"+ row +"条记录");
    }

    /**
     * 获得登录者个人信息
     * @param request
     * @return
     */
    public Result getUserInfo(HttpServletRequest request) {

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null )
            return Result.fail("获取不到个人信息");

        FamilyMember result = familyMemberMapper.selectById(familyMember.getId());
        if (result != null)
            return Result.ok("查询成功", result);
        return Result.fail("查询不到该用户信息");
    }

    /**
     * 验证密码是否正确
     * @param password
     * @param request
     * @return
     */
    public Result verifyPassword(String password, HttpServletRequest request) {
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null)
            return Result.fail("找不到用户信息");

        FamilyMember result = familyMemberMapper.selectById(familyMember.getId());
        if (result == null)
            return Result.fail("找不到用户信息");
        if (result.getPassword().equals(password))
            return Result.ok("验证通过");
        return Result.fail("验证不通过");
    }

    /**
     * 修改登录的用户的密码
     * @param map
     * @param request
     * @return
     */
    public Result updatePassword(Map map, HttpServletRequest request) {
        if (map == null || map.get("oldPassword")==null || map.get("newPassword")==null)
            return Result.fail("获取不到数据");
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null)
            return Result.fail("获取不到用户数据");
        log.info("-------->" + familyMember.toString());
        familyMember.setPassword((String) map.get("newPassword"));
        int row = familyMemberMapper.update(familyMember);
        if (row == 1)
            return Result.ok("密码修改成功");
        return Result.fail("密码修改失败");
    }
}
